/*
 * C6.3.2.c
 *
 *  Created on: 2012/12/18
 *      Author: ming
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rule_info.h"
#include "core.h"

static const PSZ RuleID = "C6.3.2";

static const PSZ MESSAGE_ENG =
		"Function macro parameter is not enclosed in parentheses";
static const PSZ MESSAGE_JPN = "関数マクロの仮引数が括弧で囲まれていない";
static const PSZ MESSAGE_CHS = "函数型宏的形参没有用括号包围";

static const U2 RANK = MSG_RANK_ERR;

static PRULEOUT pRuleHead;
static PRULEOUT pRuleTail;

static BOOL isInParen(AST i) {
	PSZ token = NULL;
	if (i - 1 == 0) {
		return FALSE;
	}
	token = CRepListToken.getTokenLabel(i - 1);
	if (token == NULL ) {
		return FALSE;
	}
	if (strcmp(token, "(")) {
		return FALSE;
	}
	token = CRepListToken.getTokenLabel(i + 1);
	if (token == NULL ) {
		return FALSE;
	}
	if (strcmp(token, "(")) {
		return FALSE;
	}
	return TRUE;
}

/******************************************************************
 * main logic
 *******************************************************************/
static S4 RuleMain(AST index, AST tuIdx) {
	INT result = RULE_RESULT_OK;
	PRULEOUT newNode = NULL;
	PRULEOUT tmpNode = NULL;
	PRULEOUT tmpNode1 = NULL;

	AST codeScp = 0;
	AST macroDefInfo = 0;
	AST fstRepToken = 0;
	AST lstRepToken = 0;
	AST i;

	if (!CDirectiveInfo.isDefine(index)) {
		return EXIT_SUCCESS;
	}

	macroDefInfo = CDirectiveInfo.define_getMacroInfo(index);
	if (!CMacroInfo.isFunctionMacro(macroDefInfo)) {
		return EXIT_SUCCESS;
	}

	fstRepToken = CMacroInfo.getFirstToken(macroDefInfo);
	lstRepToken = CMacroInfo.getLastToken(macroDefInfo);
	if (fstRepToken <= 0) {
		return EXIT_SUCCESS;
	}
	for (i = fstRepToken; i <= lstRepToken; i++) {
		if (CRepListToken.isParameter(i) && (!CRepListToken.isAfterSharp(i))
				&& (!CRepListToken.isAfterSharpSharp(i))
				&& (!CRepListToken.isBeforeSharpSharp(i))) {
			if (!isInParen(i)) {
				result = RULE_RESULT_NG;
				break;
			}
		}
	}

	if (result == RULE_RESULT_OK) {
		return EXIT_SUCCESS;
	}

	/*re-checked?*/
	tmpNode = pRuleHead;
	while (tmpNode != NULL ) {
		if (tmpNode->indexInfo == index) {
			if (result > RULE_RESULT_OK) {
				result = RULE_RESULT_OK;
			} else {
				if (tmpNode == pRuleHead) {
					pRuleHead = pRuleHead->next;
					free(tmpNode);
				} else if (tmpNode == pRuleTail) {
					tmpNode1->next = NULL;
					pRuleTail = tmpNode1;
					free(tmpNode);
				} else {
					tmpNode1->next = tmpNode->next;
					free(tmpNode);
				}
			}
			break;
		}
		tmpNode1 = tmpNode;
		tmpNode = tmpNode->next;
	}

	/*make result struct*/
	if (result > RULE_RESULT_OK) {
		newNode = (PRULEOUT) malloc(sizeof(RULEOUT));
		if (pRuleHead == NULL ) {
			pRuleHead = newNode;
		}

		codeScp = CDirectiveInfo.getCodeScp(index);
		newNode->indexInfo = index;
		newNode->fileIndex = CCodeScope.getFileIndex(codeScp);
		newNode->beginLine = CCodeScope.getBeginLine(codeScp);
		newNode->beginCol = CCodeScope.getBeginLineOffset(codeScp);
		newNode->endLine = CCodeScope.getEndLine(codeScp);
		newNode->endCol = CCodeScope.getEndLineOffset(codeScp);
		newNode->ruleID = RuleID;
		newNode->errRank = RANK;

		sprintf(newNode->msgBuf_eng, MESSAGE_ENG);
		sprintf(newNode->msgBuf_chs, MESSAGE_CHS);
		sprintf(newNode->msgBuf_jpn, MESSAGE_JPN);

		newNode->rfileIndex = 0;
		newNode->rlocLine = 0;
		newNode->next = NULL;

		if (pRuleTail != NULL ) {
			pRuleTail->next = newNode;
		}

		pRuleTail = newNode;
	}

	return EXIT_SUCCESS;
}

VOID C6_3_2_register(VOID) {
	PRULEMAN * pCurRule = &stRuleManager;
	PRULEMAN newRuleMan = (PRULEMAN) malloc(sizeof(RULEMAN));
	memset(newRuleMan, 0, sizeof(RULEMAN));
	newRuleMan->ruleID = RuleID;
	newRuleMan->ruleSwitch = OFF;
	newRuleMan->headOut = &pRuleHead;
	newRuleMan->tailOut = &pRuleTail;
	newRuleMan->ruleMain_cpp = RuleMain;
	if (stRuleManager == NULL ) {
		stRuleManager = newRuleMan;
	} else {
		while ((*pCurRule)->nextRule != NULL ) {
			pCurRule = &((*pCurRule)->nextRule);
		}
		(*pCurRule)->nextRule = newRuleMan;
	}
}

